package com.faiz.algorithm;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class RebuildQueue {

    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (p1, p2) -> {
            if (p1[0] != p2[0]) {
                return p2[0] - p1[0];
            } else {
                return p1[1] - p2[1];
            }
        });

        List<int[]> res = new LinkedList<>();

        for (int[] p : people) {
            res.add(p[1], p);
        }
        return res.toArray(new int[0][2]);
    }
}
